<template>
  <div class="pb50" v-loading="loading">
    <el-form size="small" ref="form" :model="form" label-width="150px" v-if="!loading">
      <!--基础设置-->
      <Basic :form="form"></Basic>
      <!--活动时间-->
      <Datetime :form="form"></Datetime>
      <!--活动商品-->
      <ProductIndex :form="form" :type="'edit'"></ProductIndex>
      <!--其它-->
      <Other :form="form"></Other>
    </el-form>

    <!--提交-->
    <div class="common-button-wrapper">
      <el-button size="small" @click="cancelFunc">取消</el-button>
    </div>
  </div>
</template>
<script>
import SeckillApi from "@/api/seckill.js";
import Basic from "./part/Basic.vue";
import Datetime from "./part/Datetime.vue";
import ProductIndex from "./part/Product.vue";
import Other from "./part/Other.vue";
import { mergeTable } from "@/utils/table.js";
export default {
  components: {
    /*基础设置*/
    Basic,
    /*活动时间*/
    Datetime,
    /*活动商品*/
    ProductIndex,
    /*其它*/
    Other,
  },
  data() {
    return {
      /*是否正在加载*/
      loading: false,
      /*表单对象*/
      form: {
        /*活动ID*/
        seckillActivityId: null,
        /*图片ID*/
        imageId: 0,
        /*活动日期*/
        startDate: "",
        endDate: "",
        /*活动时间*/
        activeTime: "",
        /*标题*/
        title: "",
        /*是否生效，默认1为生效，0为不生效*/
        status: 1,
        /*默认排序*/
        sort: 100,
        /*商品源数据*/
        tableData: [],
        /*商品列表*/
        productList: [],
      },
    };
  },
  created() {
    /*获取列表*/
    this.form.seckillActivityId = this.$route.query.id;
    this.getData();
  },
  methods: {
    /*获取数据*/
    getData() {
      let self = this;
      self.loading = true;
      SeckillApi.getActive(
        { seckillActivityId: self.form.seckillActivityId },
        true
      )
        .then((res) => {
          self.loading = false
          const { data, code } = res
          console.log('data', data)
          if (code === 1) {
            self.form.activityType = data.activityType
            self.form.title = data.title
            self.form.filePath = data.filePath
            self.form.imageId = data.imageId
            self.form.activeRange = [data.startTime, data.endTime]
            self.form.startDate = data.startTime
            self.form.endDate = data.endTime
            self.form.activeTime = [data.dayStartTime, data.dayEndTime]

            //商品列表
            const arrys = []
            if (data.productList && data.productList.length > 0) {
              data.productList.forEach(item => {
                const json = {
                  productName: item.product.productName,
                  seckillPrice: item.seckillPrice,
                  seckillStock: item.seckillStock,
                  limitNum: item.limitNum,
                  productPrice: item.productPrice,
                  seckillProductId: item.seckillProductId,
                  productId: item.productId,
                  stock: item.stock,
                  seckillActivityId: item.seckillActivityId,
                  totalSales: item.totalSales,
                  salesInitial: item.salesInitial,
                  joinNum: item.joinNum,
                  batchList: item.seckillBatch,
                  deliveryTime: item.deliveryTime,
                  minJoin: item.minJoin,
                  eachReachNum: item.eachReachNum,
                  discount: item.discount,
                  stepList: item.stepList,
                  productSkuId: item.productSkuId,
                  color: item.color,
                  sku: item.sku,
                  minBuyCount: item.minBuyCount,
                  seckillPrice: item.productPrice, //	活动价
                  seckillStock: item.productStock, //活动库存
                  productPrice: item.productPrice, //	产品售价
                  tenThousandPrice: item.tenThousandPrice,
                  lakhPrice: item.lakhPrice,
                  depositRatio: item.depositRatio,
                  stockRatio: item.stockRatio
                }
                arrys.push(json)
              })
            }
            self.form.tableData = arrys
            if (data.activityType === 2) {
              //拼团活动
              self.form.groupType = data.groupType
            }
          }


        })
        .catch((error) => {
          self.loading = false
        });
    },

    /*取消*/
    cancelFunc() {
      this.$router.back(-1);
    },
  },
};
</script>

